package com.framework.bas.service.control;

import com.framework.bas.bs.IPlmBasBS;
import com.framework.bas.param.GeneralParam;
import com.framework.bas.service.IControl;
import com.framework.bas.service.IService;
import com.framework.bas.service.ServiceFactory;
import com.framework.bas.util.PlmBasConstant;
import com.framework.bas.util.SpringUtil;
import com.framework.bas.util.UtilReflect;
import com.framework.bas.vo.BasService;

public class PlmBasControlImpl implements IControl {

	private IPlmBasBS plmBasBS;

	public IPlmBasBS getPlmBasBS() {
		return plmBasBS;
	}

	public void setPlmBasBS(IPlmBasBS plmBasBS) {
		this.plmBasBS = plmBasBS;
	}

	/**
	 * 根据交易码和参数分发控制
	 * 
	 * @param serviceCode
	 *            交易码
	 * @param param
	 *            传入参数
	 * @return object
	 */
	public Object execute(String serviceCode, GeneralParam param) {

		BasService basService = null;
		// 1.封装交易信息
		if (param.getAppServiceObject() == null) {
			basService = getPlmBasBS().queryServiceByCode(serviceCode);
			param.setValue(PlmBasConstant.APP_SERVICE_OBJECT, basService);
		} else {
			basService = param.getAppServiceObject();
		}
		// 2.解析交易
		String beanId = basService.getBeanId();
		Object result = null;
		String execFun = basService.getExecFun();
		Object beanObj = SpringUtil.getBean(beanId);
		result = UtilReflect.runAllMethod(beanObj, execFun, new Object[] { param }, new Class[] { GeneralParam.class });
//		result = ServiceFactory.getServiceInstance(beanId).runWork(param);

		return result;
	}

}
